//package cn.bugstack.xfg.dev.tech.test;
//
//import jakarta.annotation.Resource;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.io.FileUtils;
//import org.eclipse.jgit.api.Git;
//import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
//import org.junit.Test;
//import org.junit.runner.RunWith;
//import org.springframework.ai.document.Document;
//import org.springframework.ai.ollama.OllamaChatClient;
//import org.springframework.ai.reader.tika.TikaDocumentReader;
//import org.springframework.ai.transformer.splitter.TokenTextSplitter;
//import org.springframework.ai.vectorstore.PgVectorStore;
//import org.springframework.ai.vectorstore.SimpleVectorStore;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.core.io.PathResource;
//import org.springframework.test.context.junit4.SpringRunner;
//
//import java.io.File;
//import java.io.IOException;
//import java.nio.file.*;
//import java.nio.file.attribute.BasicFileAttributes;
//import java.util.List;
//
//@Slf4j
//@RunWith(SpringRunner.class)
//@SpringBootTest
//public class JGitTest {
//
//    @Resource
//    private OllamaChatClient ollamaChatClient;
//    @Resource
//    private TokenTextSplitter tokenTextSplitter;
//    @Resource
//    private SimpleVectorStore simpleVectorStore;
//    @Resource
//    private PgVectorStore pgVectorStore;
//
//    @Test
//    public void test() throws Exception {
//        //我的 Agent 代码自动评审项目 地址
//        String repoURL = "https://gitcode.com/qq_45826579/openai-code-review.git";
//        String username = "18522892770";
//        String password = "";
//
//        String localPath = "./cloned-repo";
//        log.info("克隆路径：" + new File(localPath).getAbsolutePath());
//
//        FileUtils.deleteDirectory(new File(localPath));
//
//        Git git = Git.cloneRepository()
//                .setURI(repoURL)
//                .setDirectory(new File(localPath))
//                .setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password))
//                .call();
//
//        git.close();
//    }
//
//    @Test
//    public void test_file() throws IOException {
//        Files.walkFileTree(Paths.get("./cloned-repo"), new SimpleFileVisitor<>() {
//            @Override
//            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
//
//                log.info("文件路径:{}", file.toString());
//                try {
//                    PathResource resource = new PathResource(file);
//                    TikaDocumentReader reader = new TikaDocumentReader(resource);
//
//                    List<Document> documents = reader.get();
//                    List<Document> documentSplitterList = tokenTextSplitter.apply(documents);
//
//                    documents.forEach(doc -> doc.getMetadata().put("knowledge", "openai-code-review"));
//                    documentSplitterList.forEach(doc -> doc.getMetadata().put("knowledge", "openai-code-review"));
//
//                    pgVectorStore.accept(documentSplitterList);
//                }catch (Exception e){
//                    log.error("遍历解析路径，上传知识库失败:{}", file.getFileName());
//                }
//
//                return FileVisitResult.CONTINUE;
//            }
//        });
//    }
//
//}
